//********************************************************
// Board.java 
// Date: 5/5/2012
// Author: Andrew Landphier
//********************************************************
// Driver to run game
//*******************************************************
// EDITED by Jeff:
//*******************************************************
package driver;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import ui.Board;
import ui.Tile;
import functionality.Player;
import functionality.RobberKnights;


public class Driver implements Serializable{
	private static JFrame frame;
	
	//Static references to the Tile Types
	//Should be used for creating landscape types (will be used to associate tiles with the proper ICONS)
	public static final String FOREST = "forest";
	public static final String CASTLE = "castle";
	public static final String TOWN = "town";
	public static final String VILLAGE = "village";
	public static final String PLAIN = "plain";
	public static final String LAKE = "lake";
	public static final String MOUNTAIN = "mountain";
	public static final String NONE = "none";
	public static final String EAST = "East";
	public static final String WEST = "West";
	public static final String SOUTH = "South";
	public static final String NORTH = "North";	
	//public static Player currentPlayer;
	protected  static int countTurn=1;//1=p1,2=p2, 3=p4, 4=p4 
	protected  static int totalTurns=1;
	protected static int _size=0;
	protected  static int turns=0;
	public static JLabel win;
	public static RobberKnights rb;
	public static Board b;
	private static int p1Points=0;
	private static int p2Points=0;
	private static int p3Points=0;
	private static int p4Points=0;
	public static int placetile_lock = 0;
	private static boolean isDone=false;
	
	public static int getSize()
	{
		return _size;
	}

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException 
	{
		
		rb=new RobberKnights();
		rb.StartCurrentGame();
		while(_size==0)
		{
			_size=rb.getSize();
		}
		rb.MakeCurrentPlayers();
		
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					b = new Board(_size);
					b.Open();
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		
		if(_size==2)

		{
			while(rb.getPlayer(0).hasTileLeft()||rb.getPlayer(1).hasTileLeft()||rb.getPlayer(0).getNumCardsInHand()>0||rb.getPlayer(1).getNumCardsInHand()>0)
			{
					if(countTurn==1)
					{						
						rb.setCurrentPlayer(0);
					}
					else if(countTurn==2)
					{						
						rb.setCurrentPlayer(1);
					}
					

				
			}
			
			isDone=true;
			//calculate points
			while(b.boardIsEnabled())
			{
				
					while(!rb.isAllDone())
					{
						
					}
					b.disable();				
				
			}
			if(!b.boardIsEnabled()){
			ArrayList<Tile> x=b.getAllTiles();		
			
				for(Tile t: x)
				{
					if(t.getTopKnight().equals(rb.getPlayer(0).getColor()))
					{
						p1Points+=t.getWorth();
					}
					else if(t.getTopKnight().equals(rb.getPlayer(1).getColor()))
					{
						p2Points+=t.getWorth();
					}
				}

				//determine winner
				if(p1Points>p2Points)
				{
					win=new JLabel("Player 1 WINS");				
				}
				else if(p1Points<p2Points)
				{
					win=new JLabel("Player 2 WINS");					

				}
				else 
				{
					win=new JLabel("Players Draw");				

				}


			}
			
		}
		else if(_size==3)

		{
			
			while(rb.getPlayer(0).getNumCardsInHand()>0||rb.getPlayer(1).getNumCardsInHand()>0||rb.getPlayer(2).getNumCardsInHand()>0||rb.getPlayer(0).hasTileLeft()||rb.getPlayer(1).hasTileLeft()||rb.getPlayer(2).hasTileLeft())
			{

				if(countTurn==1)
				{
					rb.setCurrentPlayer(0);
				}
				else if(countTurn==2)
				{
					rb.setCurrentPlayer(1);					
				}
				else if(countTurn==3)
				{
					rb.setCurrentPlayer(2);
				}
				
					
			}
			
			isDone=true;
			//calculate points
			while(b.boardIsEnabled())
			{
				
					while(!rb.isAllDone())
					{
						
					}
					b.disable();				
				
			}
			//calculate points
			//b.disable();
			if(!b.boardIsEnabled()){
				ArrayList<Tile> x=b.getAllTiles();


				for(Tile t: x)
				{
					if(t.getTopKnight().equals(rb.getPlayer(0).getColor()))
					{
						p1Points+=t.getWorth();
					}
					else if(t.getTopKnight().equals(rb.getPlayer(1).getColor()))
					{
						p2Points+=t.getWorth();
					}
					else if (t.getTopKnight().equals(rb.getPlayer(2).getColor()))
					{
						p3Points+=t.getWorth();
					}
				}


				//determine winner
				if(p1Points>p2Points&&p1Points>p3Points)
				{
					win=new JLabel("Player 1 WINS");
					
				}
				else if(p2Points>p1Points&&p2Points>p3Points)
				{
					win=new JLabel("Player 2 WINS");
					
				}
				else if(p3Points>p1Points&&p3Points>p2Points)
				{
					win=new JLabel("Player 3 WINS");
					
				}
				else 
				{
					win=new JLabel("Players Draw");
					
				}
			}
		}
		else if(_size==4)

		{
			
			while(rb.getPlayer(0).getNumCardsInHand()>0||rb.getPlayer(1).getNumCardsInHand()>0||rb.getPlayer(2).getNumCardsInHand()>0||rb.getPlayer(3).getNumCardsInHand()>0||rb.getPlayer(0).hasTileLeft()||rb.getPlayer(1).hasTileLeft()||rb.getPlayer(2).hasTileLeft()||rb.getPlayer(3).hasTileLeft())
			{
				if(countTurn==1)
				{					
					rb.setCurrentPlayer(0);					
				}
				else if(countTurn==2)
				{				
					rb.setCurrentPlayer(1);					
				}
				else if(countTurn==3)
				{					
					rb.setCurrentPlayer(2);
				}
				else if(countTurn==4)
				{				
					rb.setCurrentPlayer(3);
				}
			}
			
			
			isDone=true;
			//calculate points
			while(b.boardIsEnabled())
			{
				
					while(!rb.isAllDone())
					{
						
					}
					b.disable();				
				
			}
			
			
			//calculate winner
			if(!b.boardIsEnabled()){
				ArrayList<Tile> x=b.getAllTiles();
				for(Tile t: x)
				{
					if(t.getTopKnight().equals(rb.getPlayer(0).getColor()))
					{
						p1Points+=t.getWorth();
					}
					else if(t.getTopKnight().equals(rb.getPlayer(1).getColor()))
					{
						p2Points+=t.getWorth();
					}
					else if (t.getTopKnight().equals(rb.getPlayer(2).getColor()))
					{
						p3Points+=t.getWorth();
					}
					else if (t.getTopKnight().equals(rb.getPlayer(3).getColor()))
					{
						p4Points+=t.getWorth();
					}
				}


				//determine winner
				if(p1Points>p2Points&&p1Points>p3Points&&p1Points>p4Points)
				{
					win=new JLabel("Player 1 WINS");				
				}
				else if(p2Points>p1Points&&p2Points>p3Points&&p2Points>p4Points)
				{
					win=new JLabel("Player 2 WINS");				
				}
				else if(p3Points>p1Points&&p3Points>p2Points&&p3Points>p4Points)
				{
					win=new JLabel("Player 3 WINS");				
				}
				else if(p4Points>p1Points&&p4Points>p2Points&&p4Points>p3Points)
				{
					win=new JLabel("Player 4 WINS");				
				}
				else 
				{
					win=new JLabel("Players Draw");
				}
			}
		
		}
		//contentPane xx=new contentPane();
		//b.close();
		
		frame=new JFrame();
		
		JTextArea pts = new JTextArea(4, 4);
		pts.setEditable(false);
		if(_size==2)
		{
			pts.append("\n");
			pts.append("Player 1's Score: " +p1Points+ "\n");
			pts.append("Player 2's Score: " +p2Points+ "\n");
		}
		else if(_size==3)
		{
			pts.append("\n");
			pts.append("Player 1's Score: " +p1Points+ "\n");
			pts.append("Player 2's Score: " +p2Points+ "\n");
			pts.append("Player 3's Score: " +p3Points+ "\n");
		}
		else if(_size==4)
		{
			pts.append("\n");
			pts.append("Player 1's Score: " +p1Points+ "\n");
			pts.append("Player 2's Score: " +p2Points+ "\n");
			pts.append("Player 3's Score: " +p3Points+ "\n");
			pts.append("Player 4's Score: " +p4Points+ "\n");
		}
		JLabel mes=new JLabel("GAME OVER");
		frame.add(mes, BorderLayout.NORTH);
		frame.add(win, BorderLayout.CENTER);
		frame.add(pts, BorderLayout.SOUTH);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		//b.close();				
	}
	
	public static void setTurn()
	{
		if(getSize()==2)
		{
			if(countTurn==1)
			{
				if(rb.getPlayer(1).hasTileLeft()||Driver.rb.getPlayer(1).getNumCardsInHand()>0){
					countTurn=2;
				}
				//Driver.countTurn=2;
				turns=0;

			}
			else if(countTurn==2)
			{
				if(rb.getPlayer(0).hasTileLeft()||Driver.rb.getPlayer(0).getNumCardsInHand()>0){
					countTurn=1;
				}
				turns=0;
			}
		}
		else if(Driver.getSize()==3)
		{
			if(countTurn==1)
			{
				if(rb.getPlayer(1).hasTileLeft()||Driver.rb.getPlayer(1).getNumCardsInHand()>0){
					countTurn=2;
				}
				else if(Driver.rb.getPlayer(3).hasTileLeft()||Driver.rb.getPlayer(3).getNumCardsInHand()>0){
					countTurn=3;
				}
				turns=0;
			}
			else if(countTurn==2)
			{
				if(rb.getPlayer(2).hasTileLeft()||Driver.rb.getPlayer(2).getNumCardsInHand()>0){
					countTurn=3;
				}
				else if(Driver.rb.getPlayer(0).hasTileLeft()||Driver.rb.getPlayer(0).getNumCardsInHand()>0){
					countTurn=1;
				}
				turns=0;
			}
			else if(countTurn==3)
			{
				if(Driver.rb.getPlayer(0).hasTileLeft()||Driver.rb.getPlayer(0).getNumCardsInHand()>0){
					countTurn=1;
				}
				else if(Driver.rb.getPlayer(1).hasTileLeft()||Driver.rb.getPlayer(1).getNumCardsInHand()>0){
					countTurn=2;
				}
				turns=0;
			}


		}
		else if(getSize()==4)
		{
			if(countTurn==1)
			{
				if(rb.getPlayer(1).hasTileLeft()||Driver.rb.getPlayer(1).getNumCardsInHand()>0){
					countTurn=2;
				}
				else if(rb.getPlayer(2).hasTileLeft()||Driver.rb.getPlayer(2).getNumCardsInHand()>0){
					countTurn=3;
				}
				else if(rb.getPlayer(3).hasTileLeft()||Driver.rb.getPlayer(3).getNumCardsInHand()>0){
					countTurn=4;
				}
				turns=0;
			}
			else if(countTurn==2)
			{
				if(rb.getPlayer(2).hasTileLeft()||Driver.rb.getPlayer(2).getNumCardsInHand()>0){
					countTurn=3;
				}
				else if(rb.getPlayer(3).hasTileLeft()||Driver.rb.getPlayer(3).getNumCardsInHand()>0){
					countTurn=4;
				}
				else if(rb.getPlayer(0).hasTileLeft()||Driver.rb.getPlayer(0).getNumCardsInHand()>0){
					countTurn=1;
				}
				turns=0;
			}
			else if(countTurn==3)
			{
				if(rb.getPlayer(3).hasTileLeft()||Driver.rb.getPlayer(3).getNumCardsInHand()>0){
					countTurn=4;
				}
				else if(rb.getPlayer(0).hasTileLeft()||Driver.rb.getPlayer(0).getNumCardsInHand()>0){
					countTurn=1;
				}
				else if(rb.getPlayer(2).hasTileLeft()||Driver.rb.getPlayer(2).getNumCardsInHand()>0){
					countTurn=2;
				}
				turns=0;
			}
			else if(countTurn==4)
			{
				if(rb.getPlayer(0).hasTileLeft()||Driver.rb.getPlayer(0).getNumCardsInHand()>0){
					countTurn=1;
				}
				else if(rb.getPlayer(1).hasTileLeft()||Driver.rb.getPlayer(1).getNumCardsInHand()>0){
					countTurn=2;
				}
				else if(rb.getPlayer(2).hasTileLeft()||Driver.rb.getPlayer(2).getNumCardsInHand()>0){
					countTurn=3;
				}
				turns=0;
			}
		}
		
	}
	
	/**
	 * set turns
	 * @param int x
	 * @return void
	 */
	public static void setTurnInt(int x)
	{
		turns=x;
	}
	
	/**
	 * set turns
	 * @param int x
	 * @return void
	 */
	public static void incTurn()
	{
		++turns;
	}

	/**
	 * increment total turns
	 * @param int x
	 * @return void
	 */
	public static void incTotalTurn() {
		
		++totalTurns;
		
	}
	/**
	 * get current player
	 * @param int x
	 * @return void
	 */
	public static Player getCurrentPlayer()
	{
		return rb.getCurrentPlayer();
	}
	/**
	 * get totalturns
	 * @param int x
	 * @return void
	 */
	public static int getTotalTurns()
	{
		return totalTurns;
	}
	/**
	 * get turns
	 * @param int x
	 * @return void
	 */
	public static int getTurns()
	{
		return turns;
	}
	/**
	 * get count of turns had by player
	 * @param int x
	 * @return void
	 */
	public static int getCountTurns()
	{
		return countTurn;
	}
	/**
	 * get state of game, if all tiles laid or not
	 * @param int x
	 * @return void
	 */
	public static boolean done()
	{
		return isDone;
		
	}
	/**
	 * get is ALL done
	 * @param int x
	 * @return void
	 */
	public static void allDone() {
		// TODO Auto-generated method stub
		rb.setAllDone(true);
		
	}
	
}	